package com.cheetah.start.module.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheetah.start.common.constant.SwaggerConst;
import com.cheetah.start.common.query.PageQuery;
import com.cheetah.start.module.system.domain.SysSet;
import com.cheetah.start.module.system.mapper.SysSetMapper;
import com.cheetah.start.module.system.service.SysSetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SysSetServiceImpl extends ServiceImpl<SysSetMapper, SysSet> implements SysSetService {

    @Override
    public boolean saveOrUpdateByKV(String key, String val) {
        SysSet sysSet = getByKey(key, true);
        if (sysSet == null) {
            return save(new SysSet(key, val));
        } else {
            LambdaUpdateWrapper<SysSet> updateWrapper = getUpdateWrapper();
            updateWrapper.eq(SysSet::getSetKey, key)
                    .set(SysSet::getSetValue, val);
            return update(updateWrapper);
        }
    }

    @Override
    public boolean saveOrUpdateByKey(SysSet sysSet) {
        SysSet set = getByKey(sysSet.getSetKey(), true);
        if (set == null) {
            return save(sysSet);
        } else {
            LambdaUpdateWrapper<SysSet> updateWrapper = getUpdateWrapper();
            updateWrapper.eq(SysSet::getSetKey, sysSet.getSetKey())
                    .set(SysSet::getSetValue, sysSet.getSetValue())
                    .set(SysSet::getRemark, sysSet.getRemark());
            return update(updateWrapper);
        }
    }

    @Override
    public SysSet getByKey(String key, boolean allowNull) {
        LambdaQueryWrapper<SysSet> queryWrapper = getQueryWrapper(null);
        queryWrapper.eq(SysSet::getSetKey, key);
        SysSet one = getOne(queryWrapper);
        if (allowNull) {
            return one;
        } else {
            return one == null ? new SysSet(key) : one;
        }
    }

    @Override
    public boolean allowSwagger() {
        SysSet sysSet = getByKey(SwaggerConst.SWAGGER, false);
        String setValue = sysSet.getSetValue();
        return SwaggerConst.ON.equals(setValue);
    }

    @Override
    public boolean removeByKey(String key) {
        LambdaQueryWrapper<SysSet> queryWrapper = getQueryWrapper(null);
        queryWrapper.eq(SysSet::getSetKey, key);
        return remove(queryWrapper);
    }


    @Override
    public boolean saveBatchByKey(List<SysSet> list) {
        if (CollUtil.isNotEmpty(list)) {
            for (SysSet sysSet : list) {
                saveOrUpdateByKV(sysSet.getSetKey(), sysSet.getSetValue());
            }
            return true;
        }
        return false;
    }

    @Override
    public Page<SysSet> pageList(PageQuery pageQuery, SysSet sysSet) {
        Page<SysSet> setPage = new Page<>(pageQuery.getPage(), pageQuery.getLimit());
        LambdaQueryWrapper<SysSet> queryWrapper = getQueryWrapper(sysSet);
        page(setPage, queryWrapper);
        return setPage;
    }

    private LambdaQueryWrapper<SysSet> getQueryWrapper(SysSet sysSet) {
        LambdaQueryWrapper<SysSet> queryWrapper = Wrappers.lambdaQuery(SysSet.class);
        if (sysSet != null) {
            String setKey = sysSet.getSetKey();
            if (StrUtil.isNotBlank(setKey)) {
                queryWrapper.like(SysSet::getSetKey, setKey);
            }
            String setValue = sysSet.getSetValue();
            if (StrUtil.isNotBlank(setValue)) {
                queryWrapper.like(SysSet::getSetValue, setValue);
            }
        }
        queryWrapper.orderByDesc(SysSet::getCreateTime);
        return queryWrapper;
    }

    private LambdaUpdateWrapper<SysSet> getUpdateWrapper() {
        return Wrappers.lambdaUpdate(SysSet.class);
    }


}
